package com.zyl.javabasic.守护线程;

import lombok.extern.slf4j.Slf4j;

/**
 * @author zyl
 * @description
 * @since 2025/8/5
 */
@Slf4j
public class DaemonThreadDemo {
    public static void main(String[] args) {
        Thread userThread = new Thread(() -> {
            System.out.println("用户线程开始运行...");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info("用户线程结束运行");
        });

        Thread daemonThread = new Thread(() -> {
            while (true) {
                log.info("守护线程正在运行...");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        daemonThread.setDaemon(true);

        userThread.start();
        daemonThread.start();

        log.info("主线程结束");
    }
}